package calendar;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import common.ClampFactory;


public class CalendarMyba {
	
	private SqlSession session;//드라이버 로딩과 Connection 객체가 생성된 상태
	CalendarVo cv;
	
	public CalendarMyba() {
		session = ClampFactory.getFactory().openSession();
	}
	
	public CalendarVo getCv() {
		return cv;
	}
	public void setCv(CalendarVo cv) {
		this.cv = cv;
	}

	//일정관리 등록
	public int insert(){
		int rs = 0;
		try {
			rs = session.insert("calendar.insert", cv);
			if(rs>0){
				session.commit();
			}else{
				session.rollback();
			}
			//session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	//일정관리 삭제
	public int delete(){
		int rs = 0;
		try {
			rs = session.insert("calendar.delete", cv);
			if(rs>0){
				session.commit();
			}else{
				session.rollback();
			}
			//session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	//일정관리 메인 화면 달력용 목룍
	public List<CalendarVo> list(){
		List<CalendarVo> lists = new ArrayList<CalendarVo>();
		lists = session.selectList("calendar.list");
		session.close();
		return lists;
	}
	
	//일정관리 게시판 목록
	public ArrayList<CalendarVo> select(){
		ArrayList<CalendarVo> lists = null;
		if(cv.getCal_search_name() == null) cv.setCal_search_name("0");
		if(cv.getCal_search_title() == null) cv.setCal_search_title("");
		if(cv.getCal_date_start() == null) cv.setCal_date_start("");
		if(cv.getCal_date_end() == null) cv.setCal_date_end("");
		
		try {
			lists = (ArrayList<CalendarVo>)session.selectList("calendar.select",cv);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//session.close();
		return lists;
	}
	
	//일정관리 목록용 행의 수 반환 메서드
	public int totalRow(){
		int row = 0;
		try {
			if(cv.getCal_search_name() == null) cv.setCal_search_name("0");
			if(cv.getCal_search_title() == null) cv.setCal_search_title("");
			if(cv.getCal_date_start() == null) cv.setCal_date_start("");
			if(cv.getCal_date_end() == null) cv.setCal_date_end("");
			row = (int)session.selectOne("calnedar.totRow", cv);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}
	
	//일정관리 상세보기
	public CalendarVo view(){
		CalendarVo vo =  null;
		try {
			vo = (CalendarVo)session.selectOne("calendar.view",cv);
			//System.out.println(vo.getCal_no());
			//session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}
	
	//일정관리 수정
	public int update(){
		int rs = 0;
		try {
			rs = session.insert("calendar.update", cv);
			if(rs>0){
				session.commit();
			}else{
				session.rollback();
			}
			//session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	
}
